<template>
	<view style="width: 100vw;height: 90vh;padding-top:10vh;background-color: rgba(0, 0, 0, 0.1);">
		<view
			style="color:black;font-weight: bold;font-size: 20rpx;text-align: center;position: absolute;top: 20rpx;left: 40%;right: 40%;top:10vh">
			{{time}}
		</view>
		<view style="display: flex;width: 100vw;height:70vh;">
			<view class="left_view">
				<view v-if="team1info && team1info.title" style="font-size: 16px;">{{team1info.title}}
					<text v-if="set.strong==1 && team1info.rate>0">强化{{team1info.rate}}%</text>
				</view>
				<view style="display: flex;flex-wrap: wrap;align-items: center;justify-content: center;width: 100%;"
					v-if="team1 && team1.length>0">
					<view v-for="(item,index) in team1" class="person_view"
						:style="{border:current_team1_index==index?'4rpx solid #fff':'none',marginRight:index==team1.length-1?'':'20rpx'}">
						<view v-if="item" @click="changeP(0,index)">{{item.user_name}}</view>
						<!-- <view v-else @click="addP(0,index)">+</view> -->
					</view>
				</view>
				<view style="display: flex;position: relative;width: 100%;justify-content: center;top: 0;"
					@click="add(0)">
					<view style="position: absolute;top: 0;left: 80rpx;display: flex;height: 30rpx;text-align: center;">
						<view v-if="score.wintime1>0" style="font-size: 20rpx;">连赢{{score.wintime1}}次</view>
						<view v-if="score.winround1>0" style="font-size: 20rpx;margin-left: 80rpx;">
							{{score.winround1}}轮
						</view>
					</view>
					<view style="position: absolute;top: 20rpx;left: 0;z-index: 1;width: 80rpx;" @click.stop="back(0)">
						<view
							style="font-size: 18rpx;font-weight: bold;background-color: #999;width: 80rpx;height: 40rpx;display: flex;justify-content: center;align-items: center;">
							退一步
						</view>
					</view>
					<view style="display: flex;">
						<view style="font-size: 160px;">
							{{score.num}}
						</view>
						<view v-if="score.winround1>0 && set.count_type==1"
							style="font-size: 30px;color: black;font-weight: bold;margin-top: 30px;">
							+{{tempnum1}}/{{score.winround1+1}}
						</view>
					</view>
				</view>
			</view>
			<view class="left_view">
				<view v-if="team2info && team2info.title" style="font-size: 16px;">{{team2info.title}}
					<text v-if="set.strong==1 && team2info.rate>0">强化{{team2info.rate}}%</text>
				</view>
				<view style="display: flex;flex-wrap: wrap;align-items: center;width: 100%;justify-content: center;">
					<view v-for="(item,index) in team2" class="person_view"
						:style="{border:current_team2_index==index?'4rpx solid #fff':'none',marginRight:index==team2.length-1?'':'20rpx'}">
						<view v-if="item" @click="changeP(1,index)">{{item.user_name}}</view>
						<!-- <view v-else @click="addP(1,index)">+</view> -->
					</view>
				</view>
				<view style="display: flex;position: relative;width: 100%;justify-content: center;top: 0;"
					@click="add(1)">
					<view style="position: absolute;top: 0;left: 80rpx;display: flex;height: 30rpx;text-align: center;">
						<view v-if="score.wintime2>0" style="font-size: 20rpx;">连赢{{score.wintime2}}次</view>
						<view v-if="score.winround2>0" style="font-size: 20rpx;margin-left: 80rpx;">
							{{score.winround2}}轮
						</view>
					</view>
					<view style="position: absolute;top: 20rpx;right: 0;z-index: 1;width: 80rpx;" @click.stop="back(1)">
						<view
							style="font-size: 18rpx;font-weight: bold;background-color: #999;width: 80rpx;height: 40rpx;display: flex;justify-content: center;align-items: center;">
							退一步
						</view>
					</view>
					<view style="display: flex;">
						<view style="font-size: 160px;position: relative;">
							{{score.num1}}
						</view>
						<view v-if="score.winround2>0 && set.count_type==1"
							style="font-size: 30px;color: black;font-weight: bold;margin-top: 30px;">
							+{{tempnum2}}/{{score.winround2+1}}
						</view>
					</view>
				</view>
			</view>
		</view>
		<view style="display: flex;justify-content: center;width: 90vw;margin-left: 5vw;margin-right: 5vw;">
			<!-- <view class="person_view" @click="delp()" style="margin: 0 20rpx;">-</view> -->
			<view class="person_view" @click="plist" style="margin: 0 20rpx;">+</view>
			<view style="width: 50%;display: flex;">
				<scroll-view scroll-x style="height: 60rpx;width: 100%;white-space: nowrap;">
					<view v-for="(item,index) in team3" @click="selectP(index)" class="person_view"
						style="display: inline-block;text-align: center;line-height: 60rpx;"
						:style="{border:current_team3_index==index?'4rpx solid #fff':'none',marginRight:index==team3.length-1?'':'20rpx'}">
						{{item.user_name}}
					</view>
				</scroll-view>
			</view>

			<!-- <view @click="changeCount" class="bottom_btn" style="margin-left: 20rpx;">
				{{count_name}}
			</view> -->
			<view @click="speaknum" class="bottom_btn" style="margin-left: 20rpx;width: 100rpx;font-size: 16rpx;">
				<text style="margin-right: 6rpx;">{{set.num_count}}</text>
				<text v-if="set.count_type==1">*</text>
				<text v-if="set.count_type==2">-</text>
				<text v-if="set.strong==1" style="margin-left: 6rpx;">强化</text>
			</view>
		</view>
	</view>
</template>

<script>
	// #ifdef APP
	const tts = uni.requireNativePlugin("nrb-tts-plugin")
	// #endif
	import api from '@/common/js/request.js'
	export default {
		data() {
			return {
				num: 0,
				num1: 0,
				old_num: 0, //上一次计数_左边
				old_num1: 0, //上一次计数_右边
				list: [],
				team1: [],
				current_team1_index: -1,
				team2: [],
				current_team2_index: -1,
				team3: [],
				current_team3_index: -1,
				p1: '',
				// p2:'',
				result: 0, //输赢 左赢1 右赢2
				type: 2, //2：双打默认 1：单打
				num_count: 5, //5个球 10个球
				count_type: 2, //1乘法算法 2减法算法
				click: 0,
				count: 1, //1：累加赢球默认 0：累加输球
				count_name: '得分',
				wintime1: 0,
				wintime2: 0,
				winround1: 0,
				winround2: 0,
				score: {
					num: 0,
					num1: 0,
					wintime1: 0,
					wintime2: 0,
					winround1: 0,
					winround2: 0,
				},
				tempnum1: 0,
				tempnum2: 0,
				time: '',
				timer: null,
				team1info: '',
				team2info: '',
				strong: 1, //强化
				critical: 0,
				set: ''
			}
		},
		onLoad(options) {
			console.log('index-onLoad')
			// #ifdef APP
			this.initTTS();
			// #endif
			// if (options.do) {
			// if (uni.getStorageSync('list')) {
			// 	this.list = uni.getStorageSync('list')
			// }
			// console.log(this.list)

			// for (let i = 0; i < this.list.length; i++) {
			// 	if (this.list[i].status == 1) {
			// 		if (this.team1.length < this.set.type) {
			// 			this.team1.push(this.list[i])
			// 			this.list[i].team = 1
			// 		} else if (this.team2.length < this.set.type) {
			// 			this.team2.push(this.list[i])
			// 			this.list[i].team = 2
			// 		} else {
			// 			this.team3.push(this.list[i])
			// 			this.list[i].team = 3
			// 		}
			// 	}
			// }

			// uni.setStorageSync('list', this.list)
			// console.log('team1', this.team1)
			// console.log('team2', this.team2)
			// console.log('team3', this.team3)
			// uni.setStorageSync('team3', this.team3)
			// }
		},
		onShow() {
			console.log('index-onShow')
			if (uni.getStorageSync('set')) {
				this.set = uni.getStorageSync('set');
			}
			console.log(this.set)

			if (uni.getStorageSync('list')) {
				this.list = uni.getStorageSync('list');
			}
			console.log(this.list)

			if (uni.getStorageSync('team3')) {
				this.team3 = uni.getStorageSync('team3');
			}
			// else {
			// 	this.team3 = [];
			// 	for (let i = 0; i < this.list.length; i++) {
			// 		if (this.list[i].team == 3 && this.list[i].status == 1) {
			// 			this.team3.push(this.list[i])
			// 		}
			// 	}
			// }
			console.log('team3', this.team3)
			if (uni.getStorageSync('team1')) {
				this.team1 = uni.getStorageSync('team1');
			}
			console.log('team1', this.team1)
			if (uni.getStorageSync('team2')) {
				this.team2 = uni.getStorageSync('team2');
			}
			console.log('team2', this.team2)
			if (this.team1.length == this.set.type && this.team2.length == this.set.type) {
				this.startgame();
			}

			// if (uni.getStorageSync('team3')) {
			// 	this.team3 = uni.getStorageSync('team3');
			// }

			// if ((uni.getStorageSync('count_type') != this.count_type) ||
			// 	(uni.getStorageSync('type') != this.type) ||
			// 	(uni.getStorageSync('num_count') != this.num_count) ||
			// 	(uni.getStorageSync('strong') != this.strong)) {
			// 	if (uni.getStorageSync('count_type') != this.count_type) {
			// 		this.count_type = uni.getStorageSync('count_type')
			// 	}
			// 	if (uni.getStorageSync('type') != this.type) {
			// 		this.type = uni.getStorageSync('type')
			// 	}
			// 	if (uni.getStorageSync('num_count') != this.num_count) {
			// 		this.num_count = uni.getStorageSync('num_count')
			// 	}
			// 	if (uni.getStorageSync('strong')) {
			// 		this.strong = uni.getStorageSync('strong');
			// 	}
			// 	console.log(this.type + '打', this.num_count + '个球', 'count_type' + this.count_type, 'strong' + this.strong)

			// 	console.log(this.list)
			// 	this.team1 = [], this.team2 = [], this.team3 = [];
			// 	for (let i = 0; i < this.list.length; i++) {
			// 		if (this.list[i].status == 1) {
			// 			if (this.team1.length < this.type) {
			// 				this.team1.push(this.list[i])
			// 				this.list[i].team = 1
			// 			} else if (this.team2.length < this.type) {
			// 				this.team2.push(this.list[i])
			// 				this.list[i].team = 2
			// 			} else {
			// 				this.team3.push(this.list[i])
			// 				this.list[i].team = 3
			// 			}
			// 		}
			// 	}
			// }

			if (uni.getStorageSync('score')) {
				this.score = uni.getStorageSync('score');
			}

			// 
		},
		onHide() {
			console.log('index-onHide')
			uni.setStorageSync('score', this.score);
			uni.setStorageSync('team3', this.team3);
			uni.setStorageSync('team1', this.team1);
			uni.setStorageSync('team2', this.team2);
			uni.setStorageSync('list', this.list)
			console.log('list', this.list)
		},
		onUnload() {
			console.log('index-onUnload')
			clearInterval(this.timer);
			this.timer = null;
			uni.setStorageSync('score', this.score);
			uni.setStorageSync('team3', this.team3);
			uni.setStorageSync('team1', this.team1);
			uni.setStorageSync('team2', this.team2);
			uni.setStorageSync('list', this.list)
			console.log('list', this.list)
		},
		methods: {
			onSetTimeout() {
				let date = new Date();
				let hour = date.getHours()
				let minutes = date.getMinutes()
				let seconds = date.getSeconds()
				this.time = this.settime(hour) + "：" + this.settime(minutes) + "：" + this.settime(seconds)
				let that = this;
				this.timer = setInterval(() => {
					seconds = seconds + 1;
					if (seconds == 60) {
						minutes = minutes + 1;
						seconds = 0;
						if (minutes == 60) {
							hour = hour + 1;
							minutes = 0;
						}
					}
					that.time = that.settime(hour) + "：" + that.settime(minutes) + "：" + that.settime(seconds)
				}, 1000);
			},
			settime(time) {
				if (time < 10) {
					return "0" + time
				} else {
					return time
				}
			},
			changeCount() {
				if (this.count == 1) {
					this.count = 0
					this.count_name = '扣分'
					this.score.num = this.set.num_count
					this.score.num1 = this.set.num_count
				} else {
					this.count = 1
					this.count_name = '得分'
					this.score.num = 0
					this.score.num1 = 0
				}
				this.score.wintime1 = 0
				this.score.wintime2 = 0
				this.score.winround1 = 0
				this.score.winround2 = 0
			},
			//计分
			add(index) {
				if (this.team1.length < this.set.type || this.team2.length < this.set.type) {
					return;
				}
				if (this.set.count_type == 1) {
					//乘法
					this.ad(index)
				} else {
					//减法
					this.ad1(index)
				}
			},
			//https://www.skanda.cc/rank/game/startgame?team1=1,3&team2=3,6
			startgame() {
				let that = this;
				let team1 = [];
				let str = ''
				for (let i = 0; i < this.team1.length; i++) {
					team1.push(this.team1[i].uid)
					str = str + this.team1[i].user_name + ','
				}
				let team2 = [];
				for (let i = 0; i < this.team2.length; i++) {
					team2.push(this.team2[i].uid)
					str = str + this.team2[i].user_name + ','
				}
				// console.log(str)

				let data = {
					team1: team1.toString(),
					team2: team2.toString()
				}
				console.log(data)
				api.getNoSign('rank/game/startgame', data).then(res => {
					console.log('startgame', res)
					if (res.data.code == 1) {
						that.team1info = res.data.data.team1info;
						that.team2info = res.data.data.team2info;
					}
				})
			},
			// https://www.skanda.cc/rank/game/endgame?team1=1,3&team2=3,6&winner=team1
			endgame(winner) {
				let that = this;
				let team1 = [];
				for (let i = 0; i < this.team1.length; i++) {
					team1.push(this.team1[i].uid)
				}
				let team2 = [];
				for (let i = 0; i < this.team2.length; i++) {
					team2.push(this.team2[i].uid)
				}

				let data = {
					team1: team1.toString(),
					team2: team2.toString(),
					winner
				}
				console.log(data)
				api.getNoSign('rank/game/endgame', data).then(res => {
					console.log('endgame', res)
					if (res.data.code == 1) {

					}
				})
			},
			ad(index) {
				//乘法
				let index_temp = index;
				if (index_temp == 0) {
					//左
					this.old_num = this.score.num;
					console.log(this.score.num);
					var thisnum = this.score.num;
					var thatnum = this.score.num1;
					var str = "左"
					var teampnum = this.tempnum1;
					var winround = this.score.winround1;
				} else {
					//右
					this.old_num1 = this.score.num1;
					console.log(this.score.num1);
					var thisnum = this.score.num1;
					var thatnum = this.score.num;
					var str = "右";
					var teampnum = this.tempnum2;
					var winround = this.score.winround2;
				}



				if (this.count == 1) {

					teampnum++;
					var max = winround + 1;
					if (teampnum >= max) {
						//	thisnum = thisnum + 1; //重新计算
						thisnum = this.adnum(thisnum, str); //重新计算 暴击

						teampnum = 0;
					}
					if (thisnum >= this.set.num_count + 1) {
						//赢了
						if (str == '左') {
							this.result = 1;
						} else {
							this.result = 2;
						}
						this.start();
						return;
					}

					var msg = str + '边赢一球,'
					if (this.critical == 1) {
						msg += "双倍暴击,"
					}

					if (teampnum > 0) {
						msg = str + "边赢" + max + "分之 " + teampnum + ", "
					}

				} else {
					//扣分
					teampnum++;
					var max = winround + 1;
					if (teampnum >= max) {
						thisnum = thisnum - 1; //重新计算
						teampnum = 0;
					}


					if (thisnum == -1) {
						if (str == '左') {
							this.result = 2;
						} else {
							this.result = 1;
						}
						this.start();
						return;
					}

					var msg = str + '边输一球'

					if (teampnum > 0) {
						msg = str + "边输" + max + "分之 " + teampnum + ", "
					}
				}

				if (index_temp == 0) {
					//左
					this.score.num = thisnum;

					this.tempnum1 = teampnum;
				} else {
					//右
					this.score.num1 = thisnum;

					this.tempnum2 = teampnum;
				}
				console.log(teampnum);
				msg += this.score.num + '比' + this.score.num1;
				console.log(msg)
				if (thisnum == (this.set.num_count - 1) && thatnum == (this.set.num_count - 1)) {
					msg += ",赛点啦！,赛点啦！"
				}

				this.speak(msg)
				uni.showToast({
					title: msg,
					icon: 'none'
				});
			},

			adnum(thisnum, str) {
				let rate = 0
				if (str == '左') {
					rate = this.team1info.rate
				} else {
					rate = this.team2info.rate
				}
				var randomNumber = Math.floor(Math.random() * 100) + 1;
				if (randomNumber < rate) {
					thisnum = thisnum + 2; //重新计算
					this.critical = 1; //双倍暴击
				} else {
					thisnum = thisnum + 1; //重新计算
					this.critical = 0;
				}
				return thisnum;
			},

			ad1(index) {
				////减法
				let index_temp = index;
				if (index_temp == 0) {
					//左
					this.old_num = this.score.num;
					var thisnum = this.score.num;
					var thatnum = this.score.num1;
					var str = "左"
				} else {
					//右
					this.old_num1 = this.score.num1;
					var thisnum = this.score.num1;
					var thatnum = this.score.num;
					var str = "右";
				}

				if (this.count == 1) {
					//
					console.log()
					thisnum = this.adnum(thisnum, str); //重新计算 暴击
					if (thisnum >= this.set.num_count + 1) {
						if (str == '左') {
							this.result = 1;
						} else {
							this.result = 2;
						}
						this.start();
						return;
					}


					var msg = str + '边赢一球,'
					if (this.critical == 1) {
						msg += "双倍暴击,"
					}
				} else {
					//扣分
					thisnum = thisnum - 1; //重新计算

					if (thisnum == -1) {
						if (str == '左') {
							this.result = 2;
						} else {
							this.result = 1;
						}
						this.start();
						return;
					}

					var msg = str + '边输一球'
				}

				if (index_temp == 0) {
					//左
					this.score.num = thisnum;
				} else {
					//右
					this.score.num1 = thisnum;
				}
				msg += this.score.num + '比' + this.score.num1;
				console.log(msg)
				if (thisnum == (this.set.num_count - 1) && thatnum == (this.set.num_count - 1)) {
					msg += ",赛点啦！赛点啦！"
				}

				this.speak(msg)
				uni.showToast({
					title: msg,
					icon: 'none'
				});
			},
			// /**
			//  * 加人
			//  * @param {Object} current_p
			//  * @param {Object} index
			//  */
			// addP(index, current_p) {
			// 	console.log(index, current_p);
			// 	let that = this
			// 	let itemList = []
			// 	for (let i = 0; i < that.team3.length; i++) {
			// 		itemList.push(that.team3[i].user_name)
			// 	}
			// 	uni.showActionSheet({
			// 		itemList,
			// 		success: function(res) {
			// 			if (index == 0) {
			// 				that.team1[current_p] = that.team3[res.tapIndex]
			// 				that.formal(1, that.team3[res.tapIndex].uid);
			// 			} else {
			// 				that.team2[current_p] = that.team3[res.tapIndex]
			// 				that.formal(2, that.team3[res.tapIndex].uid);
			// 			}
			// 			that.team3.splice(res.tapIndex, 1)
			// 		},
			// 	})
			// },

			formal(team, uid) {
				for (let i = 0; i < this.list.length; i++) {
					if (this.list[i].uid == uid) {
						this.list[i].team = team
					}
				}
				console.log(team, uid, this.list)
			},

			/**
			 * 换人
			 * @param {Object} current_p
			 * @param {Object} index
			 */
			changeP(index, current_p) {
				if (this.team1.includes(null) || this.team2.includes(null)) {
					return;
				}
				if (index == 0) {
					this.current_team1_index = current_p
					this.cha(1, current_p)
				} else {
					this.current_team2_index = current_p
					this.cha(2, current_p)
				}
			},
			plist() {
				uni.navigateBack();

				// uni.navigateTo({
				// 	url: '/pages/index/person'
				// })
			},
			start() {
				if (this.count == 1) {
					this.score.num = 0;
					this.score.num1 = 0;
				} else {
					this.score.num = this.set.num_count;
					this.score.num1 = this.set.num_count;
				}
				this.old_num = this.score.num
				this.old_num1 = this.score.num1;

				let a = '';
				let b = '';
				if (this.team3.length == 0) {
					//左边赢
					if (this.result == 1) {

						this.endgame('team1')

						uni.showModal({
							title: '左边赢',
							showCancel: false
						})
						this.speak('左边赢')

						this.countround(1);

					} else if (this.result == 2) {

						this.endgame('team2')

						this.speak('右边赢')
						uni.showModal({
							title: '右边赢',
							showCancel: false
						})

						this.countround(2);
					}
				} else {
					//左边赢
					if (this.result == 1) {

						this.endgame('team1')

						a = this.team2[0];
						b = this.team3[0];
						this.formal(3, a.uid);
						this.formal(2, b.uid);
						this.team2.splice(0, 1)
						this.team3.splice(0, 1)
						this.team3.push(a)
						uni.setStorageSync('team3', this.team3)
						this.team2.push(b)
						uni.showModal({
							title: '左边赢',
							content: a.user_name + '下  ' + b.user_name + '上 ',
							showCancel: false
						})
						this.speak('左边赢' + a.user_name + '下' + b.user_name + '上 ')

						this.countround(1);

					} else if (this.result == 2) {

						this.endgame('team2')

						//右边赢
						a = this.team1[0];
						b = this.team3[0];
						this.formal(3, a.uid);
						this.formal(1, b.uid);
						this.team1.splice(0, 1)
						this.team3.splice(0, 1)
						this.team3.push(a)
						uni.setStorageSync('team3', this.team3)
						this.team1.push(b)


						this.speak('右边赢' + a.user_name + '下' + b.user_name + '上 ')
						uni.showModal({
							title: '右边赢',
							content: a.user_name + '下  ' + b.user_name + '上 ',
							showCancel: false
						})

						this.countround(2);
					}
				}
				this.startgame();
			},
			countround(index) {
				var teamcount = this.team3.length + this.set.type;
				if (index == 1) {
					this.score.wintime1 += 1; //赢次数
					var wintime = this.score.wintime1;
					var round = Math.floor(wintime / teamcount);
					this.score.winround1 = round;
					console.log('左', wintime, round);
					this.score.wintime2 = 0;
					this.score.winround2 = 0;
				} else {
					this.score.wintime2 += 1;
					var wintime = this.score.wintime2;
					var round = Math.floor(wintime / teamcount);

					this.score.winround2 = round;
					console.log('右', wintime, round);
					this.score.wintime1 = 0;
					this.score.winround1 = 0;
				}
				this.tempnum1 = 0;
				this.tempnum2 = 0;


				console.log("赢 " + round + "轮");
				if (round > 0) {
					if (this.set.count_type == 2) {
						if (this.count == 1) { //得分模式
							this.score.num = 0 + this.score.winround2;
							this.score.num1 = 0 + this.score.winround1;
						} else { //扣分模式
							this.score.num = this.set.num_count + this.score.winround2;
							this.score.num1 = this.set.num_count + this.score.winround1;
						}
						this.old_num = this.score.num
						this.old_num1 = this.score.num1;
					}
				}
			},
			/**
			 * 		注意：都上场之后才可以互换
			 * @param {Object} i 第几队
			 * @param {Object} index 第几人
			 */
			cha(i, index) {
				let that = this;
				var team = []
				if (i == 1) {
					team = this.team1;
				} else if (i == 2) {
					team = this.team2;
				} else if (i == 3) {
					team = this.team3;
				}
				//p1 如果不存在 p 放在P1里
				if (this.p1 == '') {
					if (this.team1.length < this.set.type || this.team2.length < this.set.type) {
						let item = this.team3[index];
						if (this.team1.length < this.set.type) {
							if (!this.team1.includes(item)) {
								this.team1.push(item)
								this.formal(1, item.uid)
								this.team3.splice(index, 1)
								uni.setStorageSync('team3', this.team3)
							}
						} else if (this.team2.length < this.set.type) {
							if (!this.team2.includes(item)) {
								this.team2.push(item)
								this.formal(2, item.uid)
								this.team3.splice(index, 1)
								uni.setStorageSync('team3', this.team3)
							}
							if (this.team2.length == this.set.type) {
								this.score.num = 0;
								this.score.num1 = 0
								this.startgame();
							}
						}
						that.current_team3_index = -1
					} else {
						this.p1 = {
							"team": team,
							"index": index,
							'i': i
						};
					}
				} else {

					//如果P1存在  P 放在P2  P1 P2替换 清空P1

					let a = this.p1.team[this.p1.index]
					let b = team[index]

					let a_uid = a.uid,
						b_uid = b.uid;

					if (i == 3 && this.p1.i == 3 && a_uid == b_uid) {
						uni.showModal({
							content: '确认删除' + a.user_name + '吗？',
							success(res) {
								if (res.confirm) {
									that.team3.splice(index, 1);
									that.setStatus(a_uid, 1)
									uni.setStorageSync('team3', that.team3)
								}

								that.p1 = '' //清空

								that.current_team3_index = -1
							}
						})
					} else {
						if (a_uid == b_uid) {

							this.p1 = '' //清空

							that.current_team1_index = -1
							that.current_team2_index = -1
							return;
						}
						this.p1.team[this.p1.index] = ''
						this.p1.team.splice(this.p1.index, 1, b)
						team[index] = ''
						team.splice(index, 1, a)


						// a是第一次点击，b是第二次点击
						this.formal(i, a_uid);
						this.formal(this.p1.i, b_uid);



						this.speak(a.user_name + '和' + b.user_name + '互换')
						uni.showModal({
							content: a.user_name + '和' + b.user_name + '互换',
							showCancel: false,
							success(res) {
								if (res.confirm) {
									if (i == 3 && that.p1.i == 3) {

									} else {
										that.score.num = 0;
										that.score.num1 = 0
										that.startgame()
									}

									that.p1 = '' //清空

									that.current_team1_index = -1
									that.current_team2_index = -1
									that.current_team3_index = -1
								}
							}
						})
					}

				}
			},
			setStatus(uid, status) {
				let that = this;
				let data = {
					uid,
					status
				}
				console.log(data)
				api.getNoSign('rank/users/setustatus', data).then(res => {
					console.log('羽毛球友的状态', res)
					if (res.data.code == 1) {
						for (let i = 0; i < that.list.length; i++) {
							if (that.list[i].uid == uid) {
								that.list[i].status = status
							}
						}
					}
				})
			},
			/**
			 * 互换
			 * @param {Object} index
			 */
			selectP(index) {
				// if (this.team1.includes(null) || this.team2.includes(null)) {
				// 	return;
				// }
				this.current_team3_index = index;
				this.cha(3, index)
			},
			/**
			 * 回退一步
			 * @param {Object} index
			 */
			back(index) {
				if ((this.count == 1 && this.score.num == 0 && this.score.num1 == 0) ||
					(this.count == 0 && this.score.num == (this.set.num_count) && (this.num1 == this.set.num_count))) {
					return;
				}
				if (index == 0) {
					this.score.num = this.old_num;
				} else {
					this.score.num1 = this.old_num1;
				}
				this.speak(this.score.num + '比' + this.score.num1)
			},
			speaknum() {
				if ((this.count == 1 && this.score.num == 0 && this.score.num1 == 0) ||
					(this.count == 0 && this.score.num == (this.set.num_count) && (this.score.num1 == this.set.num_count))
				) {
					return;
				}
				console.log(this.score.num + '比' + this.score.num1);
				this.speak(this.score.num + '比' + this.score.num1)
			},
			initTTS() {
				tts && tts.init({
					"lang": "ZH",
					"country": "CN"
				}, res => {
					if (res.success == 0) {
						console.log('初始化TTS成功')
					}
				})
			},
			speak(content) {
				// #ifdef APP

				//停止播放
				tts && tts.stop();

				tts && tts.speak(
					content, {}, e => {
						// console.log(e)
						if (uni.getSystemInfoSync().osName == 'android' && e.type == 'onDone') {} else if (uni
							.getSystemInfoSync().osName == 'ios' && e.type == 'didFinishSpeechUtterance') {}
					})
				// #endif
			}
		}
	}
</script>

<style>
	.btn {
		width: 100px;
		height: 100px;
		border-radius: 50px;
		background-color: red;
		color: black;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 40px;
	}

	.left_view {
		flex: 1;
		color: black;
		font-size: 40px;
		font-weight: 900;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.person_view {
		width: 100rpx;
		height: 60rpx;
		background-color: red;
		border-radius: 10rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #fff;
		font-size: 30rpx;
		font-weight: bold;
	}

	.bottom_btn {
		color: red;
		font-weight: bold;
		background-color: #f1f1f1;
		display: flex;
		justify-content: center;
		align-items: center;
		width: 100rpx;
		border-radius: 10rpx;
		font-size: 18rpx;
		height: 60rpx;
	}
</style>